package com.zrrd.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zrrd.reggie.bean.ShoppingCart;
import com.zrrd.reggie.common.BaseContext;
import com.zrrd.reggie.common.R;
import com.zrrd.reggie.mapper.ShoppingCartMapper;
import com.zrrd.reggie.service.ShoppingCartService;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.GetMapping;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class ShoppingCartServiceImpl extends ServiceImpl<ShoppingCartMapper, ShoppingCart> implements ShoppingCartService {
    @Override
    public ShoppingCart addcart(ShoppingCart shoppingCart) {
        Long currentId = BaseContext.getCurrentId();
        shoppingCart.setUserId(currentId);
        Long dishId = shoppingCart.getDishId();
        QueryWrapper<ShoppingCart> wrapper=new QueryWrapper<>();
        if (dishId!=null){
            wrapper.eq("dish_id",dishId);
        }else {
            wrapper.eq("setmeal_id",shoppingCart.getSetmealId());
        }
        ShoppingCart cart = this.getOne(wrapper);
        if (cart!=null){
            cart.setNumber(cart.getNumber()+1);
            this.updateById(cart);
        }else{
            shoppingCart.setNumber(1);
            shoppingCart.setCreateTime(LocalDateTime.now());
            this.save(shoppingCart);
            cart=shoppingCart;
        }
        return  cart;
    }



}
